$(document).ready(function() {
    loadHealthReport();
});

function loadHealthReport() {
    App.API.get('/api/passwords/health-report')
        .then(response => {
            // 更新总体健康评分
            const overallScore = document.getElementById('overallScore');
            const healthProgressBar = document.getElementById('healthProgressBar');
            const healthStatus = document.getElementById('healthStatus');
            const weakPasswordCount = document.getElementById('weakPasswordCount');
            const duplicatePasswordCount = document.getElementById('duplicatePasswordCount');

            overallScore.textContent = response.overall_score;
            healthProgressBar.style.width = `${response.overall_score}%`;

            // 设置进度条颜色
            if (response.overall_score >= 80) {
                healthProgressBar.className = 'progress-bar bg-success';
                healthStatus.textContent = '优秀';
            } else if (response.overall_score >= 60) {
                healthProgressBar.className = 'progress-bar bg-info';
                healthStatus.textContent = '良好';
            } else if (response.overall_score >= 40) {
                healthProgressBar.className = 'progress-bar bg-warning';
                healthStatus.textContent = '一般';
            } else {
                healthProgressBar.className = 'progress-bar bg-danger';
                healthStatus.textContent = '较差';
            }

            weakPasswordCount.textContent = response.weak_passwords.length;
            duplicatePasswordCount.textContent = response.duplicate_passwords.length;

            // 渲染问题密码列表
            renderProblemPasswordsTable(response.problem_passwords);
        })
        .catch(error => {
            console.error('加载密码健康报告失败:', error);
            App.Utils.showNotification('加载密码健康报告失败', 'error');
        });
}

function renderProblemPasswordsTable(problemPasswords) {
    const tableBody = document.getElementById('problemPasswordsTableBody');
    tableBody.innerHTML = '';

    problemPasswords.forEach(password => {
        const row = document.createElement('tr');

        // 设置行样式
        let rowClass = '';
        if (password.problem_type === 'weak') {
            rowClass = 'table-danger';
        } else if (password.problem_type === 'duplicate') {
            rowClass = 'table-warning';
        } else if (password.problem_type === 'expired') {
            rowClass = 'table-info';
        }

        row.className = rowClass;

        // 设置问题类型文本和建议
        let problemTypeText, recommendation;
        switch (password.problem_type) {
            case 'weak':
                problemTypeText = '弱密码';
                recommendation = '使用更强的密码，包含大小写字母、数字和特殊符号';
                break;
            case 'duplicate':
                problemTypeText = '重复密码';
                recommendation = '为不同网站使用不同的密码';
                break;
            case 'expired':
                problemTypeText = '密码过期';
                recommendation = '定期更新密码以提高安全性';
                break;
            default:
                problemTypeText = '其他问题';
                recommendation = '检查并更新密码';
        }

        row.innerHTML = `
            <td>${password.url}</td>
            <td>${password.account}</td>
            <td>${problemTypeText}</td>
            <td>${recommendation}</td>
            <td>
                <button class="btn btn-primary btn-sm" onclick="editPassword(${password.id})"><i class="fas fa-edit"></i> 修改</button>
            </td>
        `;
        tableBody.appendChild(row);
    });
}

function editPassword(passwordId) {
    window.location.href = `/admin/passwords/edit/${passwordId}`;
}